package com.service.impl;

import com.DTO.EnrollDTO;
import com.DTO.LoginDTO;
import com.Exception.NormalException.FileExceptions.FileFormatException;
import com.Exception.NormalException.UserExceptions.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.entity.User;
import com.mapper.UserMapper;
import com.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.NoSuchElementException;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author XTC
 * @since 2024-07-10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FileService fileService;

    @Override
    public User login(LoginDTO loginDTO) throws NoSuchUserException, PasswordErrorException {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", loginDTO.getPhone());
        User user = userMapper.selectOne(wrapper);
        if (user == null) {
            throw new NoSuchUserException();
        }
        if (!user.getPassword().equals(loginDTO.getPassword())) {
            throw new PasswordErrorException();
        }
        return user;
    }

    @Override
    public boolean enroll(EnrollDTO enrollDTO) throws PhoneAlreadyEnrollException {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", enrollDTO.getPhone());
        if(!this.userMapper.selectList(queryWrapper).isEmpty()) {
            throw new PhoneAlreadyEnrollException();
        }
        User user = new User();
        BeanUtils.copyProperties(enrollDTO, user);
        user.setHeadShotUrl("/head/default.jpg");
        return this.userMapper.insert(user) > 0;
    }

    @Override
    public boolean update(User user) {
        UpdateWrapper<User>  updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", user.getId());
        if(user.getName() != null) {
            updateWrapper.set("name", user.getName());
        }
        if(user.getPassword() != null) {
            updateWrapper.set("password", user.getPassword());
        }
        if(user.getGender() != null) {
            updateWrapper.set("gender", user.getGender());
        }
        if(user.getBirthday() != null) {
            updateWrapper.set("birthday", user.getBirthday());
        }
        return this.userMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public boolean updatePhone(Integer userId, String phone) throws PhoneAlreadyEnrollException, NullPhoneException {
        if(phone == null) {
            throw new NullPhoneException();
        }
        QueryWrapper<User> w1 = new QueryWrapper<User>().eq("phone", phone);
        if(!this.userMapper.selectList(w1).isEmpty()) {
            throw new PhoneAlreadyEnrollException();
        }
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", userId).set("phone", phone);
        return this.userMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public String uploadHead(Integer userId, MultipartFile head) throws FileFormatException, IOException {
        String url = this.fileService.saveFile(head, "head/", String.valueOf(userId), false);
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", userId).set("head_shot_url", url);
        this.userMapper.update(null, updateWrapper);
        return url;
    }
}
